package managedbean.IT;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;
import javax.ejb.EJB;
import session.stateless.EmployeeSessionBeanLocal;
import session.stateless.AccessRoleSessionBeanLocal;
import javax.faces.bean.ManagedProperty;
import entity.Employee;
import entity.AccessRole;
import java.util.ArrayList;
import java.util.Collections;
import javax.faces.application.FacesMessage;
import managedbean.UserSessionManagerBean;
import javax.faces.context.FacesContext;
import session.stateless.LogSessionBeanLocal;

@ManagedBean(name = "assignRoleManagerBean")
@RequestScoped
public class AssignRoleManagerBean {

    @ManagedProperty(value = "#{userSessionManagerBean}")
    private UserSessionManagerBean userSessionManagerBean;
    @EJB
    private EmployeeSessionBeanLocal employeeSessionBeanLocal;
    @EJB
    private AccessRoleSessionBeanLocal accessRoleSessionBeanLocal;
    @EJB
    private LogSessionBeanLocal logSessionBeanLocal;
    private ArrayList<String> rolesCurrent;
    private ArrayList<String> rolesAvailable;
    private String selectAdd;
    private String selectRemove;
    private String loadAssignRole;
    private String userId;
    private String department;
    private String designation;
    private String name;
    private static Employee employee;

    public void accessRolesManagerBean() {
    }

    public String getLoadAssignRole() {



        try {
            employee = (Employee) userSessionManagerBean.getSelectedItem();
            updateRoleList();
        } catch (Exception exception) {
            try {

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort()
                        + "/MerlionERP-war/Web_IT/searchEmployee.xhtml?request=assignRole");
            } catch (Exception ex) {
            }
        }

        return null;

    }

// <editor-fold defaultstate="collapsed" desc="Getter & Setter methods. Click on the + sign on the left to edit the code.">
    public void setLoadAssignRole(String loadAssignRole) {
        this.loadAssignRole = loadAssignRole;
    }

    public ArrayList<String> getRolesAvailable() {
        return rolesAvailable;
    }

    public void setRolesAvailable(ArrayList<String> rolesAvailable) {
        this.rolesAvailable = rolesAvailable;
    }

    public String getSelectAdd() {
        return selectAdd;
    }

    public void setSelectAdd(String selectAdd) {
        this.selectAdd = selectAdd;
    }

    public String getSelectRemove() {
        return selectRemove;
    }

    public void setSelectRemove(String selectRemove) {
        this.selectRemove = selectRemove;
    }

    public ArrayList<String> getRolesCurrent() {
        return rolesCurrent;
    }

    public void setRolesCurrent(ArrayList<String> rolesCurrent) {
        this.rolesCurrent = rolesCurrent;
    }

    public EmployeeSessionBeanLocal getEmployeeSessionBeanLocal() {
        return employeeSessionBeanLocal;
    }

    public void setEmployeeSessionLocal(EmployeeSessionBeanLocal employeeSessionBeanLocal) {
        this.employeeSessionBeanLocal = employeeSessionBeanLocal;
    }

    public UserSessionManagerBean getUserSessionManagerBean() {
        return userSessionManagerBean;
    }

    public void setUserSessionManagerBean(UserSessionManagerBean userSessionManagerBean) {
        this.userSessionManagerBean = userSessionManagerBean;
    }

    /**
     * @return the userId
     */
    public String getUserId() {
        return userId;
    }

    /**
     * @param userId the userId to set
     */
    public void setUserId(String userId) {
        this.userId = userId;
    }

    /**
     * @return the department
     */
    public String getDepartment() {
        return department;
    }

    /**
     * @param department the department to set
     */
    public void setDepartment(String department) {
        this.department = department;
    }

    /**
     * @return the designation
     */
    public String getDesignation() {
        return designation;
    }

    /**
     * @param designation the designation to set
     */
    public void setDesignation(String designation) {
        this.designation = designation;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

//</editor-fold>
    public void updateRoleList() {

        userId = employee.getUserId();
        name = employee.getName();
        department = employee.getDepartment();
        designation = employee.getDesignation();

        setRolesCurrent(employeeSessionBeanLocal.getRoles(userId));
        setRolesAvailable(new ArrayList<String>());

        boolean found = false;
        for (AccessRole a : accessRoleSessionBeanLocal.getAccessRoles()) {
            for (int i = 0; i < rolesCurrent.size(); i++) {

                if (a.getRoleType().compareTo(rolesCurrent.get(i)) == 0) {
                    found = true;
                } else {
                }

            }
            if (!found) {
                getRolesAvailable().add(a.getRoleType());
            }
            found = false;
        }

        Collections.sort(getRolesAvailable());



    }

    public void doneUpdate(ActionEvent event) {

        if (rolesCurrent.size() > 0) {
            ArrayList<String> al = new ArrayList<String>();
            al.add("Role assignment completed");
            al.add("You have successfully assigned/removed role from the account. Please be reminded that the changes will only be reflected on the next login.");
            al.add("Click on the link to return to ");
            al.add("continue");
            al.add("Web_IT/searchEmployee.xhtml?request=assignRole");
            userSessionManagerBean.setSelectedItem(al);


            try {

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort()
                        + "/MerlionERP-war/success.xhtml");
            } catch (Exception ex) {
            }
        } else {
              FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Employee need at least 1 role.", ""));
        }

    }

    public void addRole(ActionEvent event) {
        if (selectAdd != null) {
            employeeSessionBeanLocal.updateAccessRole(employee.getUserId(), selectAdd, true);
            logSessionBeanLocal.addLog("Assign access role", "Access role: " + selectAdd + " assigned to employee account: " + getUserId(), "assignRole.xhtml", userSessionManagerBean.getUserId());
        }
        updateRoleList();
    }

    public void removeRole(ActionEvent event) {
        if (selectRemove != null && rolesCurrent.size() > 0 ) {
            employeeSessionBeanLocal.updateAccessRole(employee.getUserId(), selectRemove, false);

            logSessionBeanLocal.addLog("Assign access role", "Access role: " + selectAdd + " unassigned from employee account: " + getUserId(), "assignRole.xhtml", userSessionManagerBean.getUserId());
        }
        updateRoleList();

    }
}
